package com.jt.controller;

import com.jt.pojo.Rights;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@RestController//@restcontroller其实是@controller和@responsebody的结合
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
        String token=userService.login(user);
        //StringUtils判断字符串是否存在
        if (StringUtils.hasLength(token)) {
            return SysResult.success(token);
        }else{
            return SysResult.fail();
        }
    }

    @GetMapping("/hello")
    public List<User> hello(){

        return userService.findAll();
    }


    //当put和post请求时，才要加@RequestBody注解
    @GetMapping("/list")
    public SysResult getUserList(PageResult pageResult){

        pageResult=userService.getUserList(pageResult);
        return  SysResult.success(pageResult);
    }


//    @PutMapping("/status/{id}/{status}")
//    public SysResult updateStatus(@PathVariable Integer id,@PathVariable Boolean status){
//
//        userService.updateStatus(id,status);
//        return SysResult.success();
//    }

    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(User user){

        userService.updateStatus(user);
        return SysResult.success();
    }

//    @DeleteMapping("/{id}")
//    public SysResult deleteUser(User user){
//    userService.deleteUserById(user);
//        return SysResult.success();
//    }

    @DeleteMapping("/{id}")
    public SysResult deleteUser(@PathVariable Integer id){
        userService.deleteUserById(id);
        return SysResult.success();
    }

    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        //int a=1/0;
        return SysResult.success();
    }

    @GetMapping("/{id}")
    public SysResult getUserById(@PathVariable Integer id){
        User user=userService.getUserById(id);
        return SysResult.success(user);
    }

    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }
}
